package com.roomorama.caldroid;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class RatioFrameLayout extends FrameLayout {

    boolean isRatio = true;
    protected float ratio;
    protected boolean dependOnX = true;

    public void setDependOnX(boolean bo) {
        if (dependOnX == bo) {
            return;
        }
        dependOnX = bo;
        postInvalidate();
    }

    public RatioFrameLayout(@NonNull Context context) {
        this(context, null);
    }

    public RatioFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RatioFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ratio = 1;
    }

    protected float getRatio() {
        return ratio;
    }

    public void enableRatio(boolean bo) {
        if (isRatio == bo) {
            return;
        }
        isRatio = bo;
        requestLayout();
    }

    public void updateRatio(float ratio) {
        if (this.ratio == ratio) {
            return;
        }
        this.ratio = ratio;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isRatio()) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
                getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        // 高度和宽度等比例
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                childWidthSize, MeasureSpec.EXACTLY);
        if (childWidthSize == 0) {
            int h = getMeasuredHeight();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (h / getRatio()), MeasureSpec.EXACTLY);
        } else {
            if (dependOnX) {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * getRatio()), MeasureSpec.EXACTLY);
            } else {
                int h = getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (h / getRatio()), MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        try {
            super.onLayout(changed, left, top, right, bottom);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    protected boolean isRatio() {
        return ratio != 0 && isRatio;
    }
}
